package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.constant.MapConstant;
import com.tbit.main.dao.core.ParkPointApplyDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.ParkPointApply;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.User;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2022-07-05 16:33
 * 站点审核
 */
@Service("parkPointApplyService")
public class ParkPointApplyServiceImpl implements ParkPointApplyService {

    @Autowired
    private ParkPointApplyDao parkPointApplyDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private ParkPointService parkPointService;
    @Autowired
    private UserService userService;
    @Autowired
    private DataService dataService;

    @Override
    public List<ParkPointApply> getByAccountId(String phone, Integer accountId, Double lon, Double lat, Integer radius, Integer mapType) {

        Map<String, Object> params = new HashMap<String, Object>();
        if (phone != null) {
            Integer account = dataService.getParentId(accountId);

            User user = userService.getByPhone(account, phone);
            if (user == null) {
                throw new BaseException("System.account.notExist");
            }
            params.put("userId", user.getUserId());
        }

        params.put(DaoParamConstant.accountId, accountId);
        if (radius != null) {
            Double area = 0.00001D * radius;
            Point point = new Point(lon, lat);
            point = calibrateService.commonCalibrate(point, mapType, MapConstant.MAP_ORI);
            params.put("minLo", point.getX() - area);
            params.put("maxLo", point.getX() + area);
            params.put("minLa", point.getY() - area);
            params.put("maxLa", point.getY() + area);
        }
        return parkPointApplyDao.getByAccountId(params);
    }


    @Override
    public void audit(Integer accountUserId, List<Integer> parkPointApplyId, Integer state, String handleInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountUserId", accountUserId);
        params.put("parkPointApplyIds", StringUtil.getListString(parkPointApplyId));
        params.put("state", state);
        params.put("handleInfo", handleInfo + "运维端审核通过");
        params.put("handleTime", DateTimeUtil.getNowTime());
        parkPointApplyDao.audit(params);
    }

    @Override
    public List<ParkPointApply> getParkPointApplyIds(List<Integer> parkPointApplyIds) {
        return parkPointApplyDao.getParkPointApplyIds(StringUtil.getListString(parkPointApplyIds));
    }
}
